﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CARMSBusiness;
using CARMSBusiness.InterFace;
using CARMSData.EntityClasses;

namespace CARMS.SysPage
{
    [Function("SysPage.PageGridDetail")]
    public partial class PageGridDetail : PageBase
    {
        private Guid CurrentGridColumnID
        {
            get
            {
                return GetGuidByParameter("GridColumnID");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDefault();
                LoadDefault();
            }
        }
        private void BindDefault()
        {
            //ControlHelper.BindPublicItemsByTypeID(dplShowType, DictionaryCategoryConst.GridColumnTypeID, EmptyType.None);

            ControlHelper.BindColumnTypeEntity(dplColumnType);
            ControlHelper.BindDictionaryCategory(dplPulicCategory);
        }

  

        protected void dplColumnType_OnSelectedIndexChanged(object sender, EventArgs e)
        { 
            if (dplColumnType.SelectedValue == ColumnType.ColumnPublicItem.Value)
            {

                dplPulicCategory.Visible = true;
            }
            else
            {
                dplPulicCategory.Visible = false;
            }

            if (dplColumnType.SelectedValue == ColumnType.ColumnRelativeTable.Value)
            {
                panelRelativeTable.Visible = true;
            }
            else
            {
                panelRelativeTable.Visible = false;
            }
        }


        private void LoadDefault()
        {
            var gridColumnEntity = new ActionGridColumnEntity(CurrentGridColumnID);
            if (!gridColumnEntity.IsNew)
            {
                lblActionID.Text = gridColumnEntity.ActionId;
                lblColumnName.Text = gridColumnEntity.ColumnName;
                lblColumDataType.Text = gridColumnEntity.ColumnDataType;
                lblColumnLength.Text = gridColumnEntity.ColumnLegnth.ToString();
                lblColumnNullAble.Text = gridColumnEntity.ColumnNullAble ? "True" : "False";
                lblCreateDate.Text = gridColumnEntity.CreateDate.ToString(ApplicationConfiguration.DateTimeFormat);

                chkIfSort.Checked = gridColumnEntity.IfSort;
                chkIfShow.Checked = gridColumnEntity.IfShow;

                txtHeaderText.Text = gridColumnEntity.HeaderText;
                txtSortIndex.Text = gridColumnEntity.SortIndex.ToString();

                txtTypeValue.Text = gridColumnEntity.TypeValue;

                //txtRelativeTable.Text = gridColumnEntity.RelativeTable;
                //txtRelativeColumn.Text = gridColumnEntity.RelativeTable;
                //txtRelativeSearchColumn.Text = gridColumnEntity.RelativeSearchColumn;

                //dplShowType.SelectedValue = gridColumnEntity.ShowType.ToString();
                dplColumnType.SelectedValue = gridColumnEntity.ColumnType;
                dplPulicCategory.SelectedValue = gridColumnEntity.TypeValue;

                txtRelativeTableName.Text = gridColumnEntity.RelativeTable;
                txtRelativeColumnName.Text = gridColumnEntity.RelativeColumn;
                txtRelativeSearchColumnName.Text = gridColumnEntity.RelativeSearchColumn;

                LoadColumnTypePanel(gridColumnEntity);
            }
        }


        private void LoadColumnTypePanel(ActionGridColumnEntity gridColumnEntity)
        {
            if (gridColumnEntity.ColumnType == ColumnType.ColumnPublicItem.Value)
            {

                dplPulicCategory.Visible = true;
            }
            else
            {
                dplPulicCategory.Visible = false;
            }

            if (gridColumnEntity.ColumnType == ColumnType.ColumnRelativeTable.Value)
            {
                panelRelativeTable.Visible = true;
            }
            else
            {
                panelRelativeTable.Visible = false;
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            var gridColumnEntity = new ActionGridColumnEntity(CurrentGridColumnID);
            if (!gridColumnEntity.IsNew)
            {
                gridColumnEntity.IfSort = chkIfSort.Checked;
                gridColumnEntity.IfShow = chkIfShow.Checked;
                gridColumnEntity.HeaderText = txtHeaderText.Text;
                gridColumnEntity.SortIndex = int.Parse(txtSortIndex.Text);
                gridColumnEntity.TypeValue = txtTypeValue.Text;
                //gridColumnEntity.RelativeTable = txtRelativeTable.Text;
                //gridColumnEntity.RelativeTable = txtRelativeColumn.Text;
                //gridColumnEntity.RelativeSearchColumn = txtRelativeSearchColumn.Text;
                //gridColumnEntity.ShowType = int.Parse(dplShowType.SelectedValue);
                gridColumnEntity.ColumnType = dplColumnType.SelectedValue;
                gridColumnEntity.TypeValue = dplPulicCategory.SelectedValue;
                gridColumnEntity.RelativeTable = txtRelativeTableName.Text.Trim();
                gridColumnEntity.RelativeColumn = txtRelativeColumnName.Text.Trim();
                gridColumnEntity.RelativeSearchColumn = txtRelativeSearchColumnName.Text.Trim();


                if (gridColumnEntity.Save())
                {
                    RediretResolveUrl(SiteUrls.SysPage.PageGridConfig + "?ActionID=" + gridColumnEntity.ActionId);
                }
                else
                {
                    Alert(GenerateAlert.SaveFailed);
                }
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            var gridColumnEntity = new ActionGridColumnEntity(CurrentGridColumnID);
            if (!gridColumnEntity.IsNew)
            {
                RediretResolveUrl(SiteUrls.SysPage.PageGridConfig + "?ActionID=" + gridColumnEntity.ActionId);
            }
        }
    }
}
